const { Service } = require("egg");

class VideoService extends Service {
    //添加视频
    async addVideo(body) {
        try {
            await this.app.model.Video.create({
                title: body.title,
                image: body.image,
                iframe_url: body.iframe_url
            })
            return true
        } catch (error) {
            return false
        }
    }
    //获取视频
    async getVideo(query) {
        try {
            let number = parseInt(query.page)
            let start = number*10-10
            let total = parseInt(query.total)
            const videoList = await this.app.model.Video.findAll({
                limit:[start,total]
            })
            return videoList
        } catch (error) {
            return null
        }
    }
    //删除视频
    async deleteVideo(id) {
        try {
            await this.app.model.Video.destroy({
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false
        }
    }
    //更新视频
    async updateVideo(id, body) {
        try {
            const video = {
                title: body.title,
                image: body.image,
                iframe_url: body.iframe_url
            }
            await this.app.model.Video.update(video, {
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false
        }
    }
    async getVideoDetail(id){
        try {
            let video =  await this.app.model.Video.findOne({
                where:{
                    id
                }
            })
            return video
        } catch (error) {
            return false            
        }
    }
}

module.exports = VideoService